package com.teatime.client.view;

import com.teatime.client.R;
import com.teatime.client.TeaTimeUtils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.FrameLayout.LayoutParams;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

public abstract class TPopupWindow implements OnDismissListener, OnClickListener, OnKeyListener{

	public static final int BUTTON_POSITIVE = -1;
	public static final int BUTTON_NEGATIVE = -2;
	public static final int BUTTON_BACK = -3;
	
	public static final int MODE_SAVE_CANCEL = 0;
	public static final int MODE_BACK = 1;
	
	private Context mContext;
	private PopupWindow mRep;
	private View mFrameView;
	private View mContentView;
	private View mFrameLayoutView;
	
	private Button mBtnPositive;
	private Button mBtnNegative;
	private Button mBtnBack;
	protected OnPopdownListener mPopdownListener;
	private int mMode;
	private int mClickedBtn;
	
	public TPopupWindow(Context context) {
		mContext = context;
		mMode = getMode();
		init(getTitleId(), getContentLayoutId());
		
	}
	
	abstract protected int getContentLayoutId();
	
	protected int getTitleId() {
		return -1;
	}
	
	protected int getMode() {
		return MODE_SAVE_CANCEL;
	}
	
	private void createPopupWindow() {
		mRep = new PopupWindow(mFrameView, LayoutParams.FILL_PARENT, 
				TeaTimeUtils.getWindowVisibleHeight(getContext()), true);
		mRep.setFocusable(true);
		
		// to capture the keydown event,
		// we must call setBackgroundDrawable
		mRep.setBackgroundDrawable(new BitmapDrawable());
        mRep.setOnDismissListener(this);
	}
	
	private void init(int titleId, int contentLayoutId) {
		initViews(titleId, contentLayoutId);
        mPopdownListener = null;
        mClickedBtn = 0;
	}
	
	private void initViews(int titleId, int contentLayoutId) {
		
		LayoutInflater factory = LayoutInflater.from(getContext());
		mFrameView = factory.inflate(R.layout.view_popup_win, null);
		
		ViewGroup contentContainer = (ViewGroup)mFrameView.findViewById(
				R.id.frm_popup_win_content);
		if (contentContainer != null) {
			mContentView = factory.inflate(contentLayoutId, null);
			contentContainer.addView(mContentView);
		}
		
		mBtnPositive = (Button)mFrameView.findViewById(R.id.btn_popup_win_positive);
		if (mBtnPositive != null) {
			mBtnPositive.setOnClickListener(this);
			if (mMode == MODE_BACK) {
				mBtnPositive.setVisibility(View.GONE);
			}
			else {
				mBtnPositive.setVisibility(View.VISIBLE);
			}
		}
		
		mBtnNegative = (Button)mFrameView.findViewById(R.id.btn_popup_win_negative);
		if (mBtnNegative != null) {
			mBtnNegative.setOnClickListener(this);
			if (mMode == MODE_BACK) {
				mBtnNegative.setVisibility(View.GONE);
			}
			else {
				mBtnNegative.setVisibility(View.VISIBLE);
			}
		}
		
		mBtnBack = (Button)mFrameView.findViewById(R.id.btn_popup_win_back);
		if (mBtnBack != null) {
			mBtnBack.setOnClickListener(this);
			if (mMode == MODE_BACK) {
				mBtnBack.setVisibility(View.VISIBLE);
			}
			else {
				mBtnBack.setVisibility(View.GONE);
			}
		}
		
		if (titleId > 0) {
			TextView txtTitle = (TextView)mFrameView.findViewById(R.id.txt_popup_win_title);
			if (txtTitle != null) {
				txtTitle.setText(titleId);
			}
		}
		
		mFrameLayoutView = mFrameView.findViewById(R.id.frm_popup_win);
		if (mFrameLayoutView != null) {
			mFrameLayoutView.setOnKeyListener(this);
		}
		onInitViews();
	}
	
	protected void onInitViews() {
		
	}
	
	public Context getContext() {
		return mContext;
	}
	
	public void show() {
		if (mRep == null) {
			createPopupWindow();
		}
		setControlValues();

		mRep.setAnimationStyle(R.style.PopupWinAnimation);
		mRep.showAtLocation(TeaTimeUtils.getTopView(getContext()), 
				Gravity.TOP, 0, 
				TeaTimeUtils.getWindowStatusBarHeight(getContext()));
		
	}
	
	public View findViewById(int resId) {
		if (mContentView == null) {
			return null;
		}
		
		return mContentView.findViewById(resId);
	}
	
	public void setOnPopdownListener(OnPopdownListener listener) {
		mPopdownListener = listener;
	}
	
	protected void setControlValues() {
		
	}
	
	protected void getControlValues() {
		
	}
	
	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub
		onDismiss(mClickedBtn);
	}
	
	protected void onDismiss(int which) {
		if (mPopdownListener != null) {
			mPopdownListener.onDismiss(this, which);
		}
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		mClickedBtn = BUTTON_NEGATIVE;
		if (view == mBtnPositive) {
			getControlValues();
			mClickedBtn = BUTTON_POSITIVE;
		}
		else if (view == mBtnNegative) {
			mClickedBtn = BUTTON_NEGATIVE;
		}
		else if (view == mBtnBack) {
			mClickedBtn = BUTTON_BACK;
		}
		mRep.dismiss();
	}

	
	public interface OnPopdownListener {
		public void onDismiss(TPopupWindow popup, int which);
	}


	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (v == mFrameView 
			&& event.getAction() == KeyEvent.ACTION_DOWN 
			&& keyCode == KeyEvent.KEYCODE_BACK) {
            mRep.dismiss();
            return true;
		}
		return false;
	
	}
	
}
